package dice.sinanya.db.system;

import dice.sinanya.db.tools.DbUtil;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.NotProipertiesSwitch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.*;
import java.util.ArrayList;

import static dice.sinanya.system.MessagesSystem.loginInfo;

public class GetPropertiesSwitch {
    private static final Logger log = LogManager.getLogger(GetPropertiesSwitch.class.getName());


    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public ArrayList<String> getSystemColumn() {
        ArrayList<String> systemColumn = new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "select * from test.systemProperties limit 1";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ResultSetMetaData rsmd = ps.getMetaData();
                int size = rsmd.getColumnCount();
                for (int i = 1; i <= size; i++) {
                    systemColumn.add(rsmd.getColumnName(i));
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return systemColumn;
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public ArrayList<String> getBanColumn() {
        ArrayList<String> banColumn = new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "select * from test.banProperties limit 1";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ResultSetMetaData rsmd = ps.getMetaData();
                int size = rsmd.getColumnCount();
                for (int i = 1; i <= size; i++) {
                    banColumn.add(rsmd.getColumnName(i));
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return banColumn;
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public ArrayList<String> getGameColumn() {
        ArrayList<String> gameColumn = new ArrayList<>();
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "select * from test.gameProperties limit 1";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ResultSetMetaData rsmd = ps.getMetaData();
                int size = rsmd.getColumnCount();
                for (int i = 1; i <= size; i++) {
                    gameColumn.add(rsmd.getColumnName(i));
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
        return gameColumn;
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public Object getSystemValue(String column, EntityTypeMessages entityTypeMessages) throws NotProipertiesSwitch {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "select " + column + " from test.systemProperties where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, String.valueOf(loginInfo.getLoginId()));
                try (ResultSet set = ps.executeQuery()) {
                    if (set.next()) {
                        return set.getObject(column);
                    }
                }
            }
        } catch (SQLException e) {
            throw new NotProipertiesSwitch(entityTypeMessages, e.getMessage());
        }
        throw new NotProipertiesSwitch(entityTypeMessages, "未找到列名:" + column);
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public Object getBanValue(String column, EntityTypeMessages entityTypeMessages) throws NotProipertiesSwitch {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "select " + column + " from test.banProperties where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, String.valueOf(loginInfo.getLoginId()));
                try (ResultSet set = ps.executeQuery()) {
                    if (set.next()) {
                        return set.getObject(column);
                    }
                }
            }
        } catch (SQLException e) {
            throw new NotProipertiesSwitch(entityTypeMessages, e.getMessage());
        }
        throw new NotProipertiesSwitch(entityTypeMessages, "未找到列名:" + column);
    }

    /**
     * 读取数据库中的骰点历史信息到缓存
     */
    public Object getGameValue(String column, EntityTypeMessages entityTypeMessages) throws NotProipertiesSwitch {
        try (Connection conn = DbUtil.getConnection()) {
            //language=MySQL
            String sql = "select " + column + " from test.gameProperties where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, String.valueOf(loginInfo.getLoginId()));
                try (ResultSet set = ps.executeQuery()) {
                    if (set.next()) {
                        return set.getObject(column);
                    }
                }
            }
        } catch (SQLException e) {
            throw new NotProipertiesSwitch(entityTypeMessages, e.getMessage());
        }
        throw new NotProipertiesSwitch(entityTypeMessages, "未找到列名:" + column);
    }
}
